﻿using System;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using PostProcGUI.ModelView.Commands;
using PostProcGUI.ModelView.ForModel;

namespace PostProcGUI.ModelView
{
    public class PalleteViewModel : ViewModelBase
    {
        #region Поля


        private string _name;
        private ObservableCollection<ColorLevelViewModel> _pallete = new ObservableCollection<ColorLevelViewModel>();
        private readonly ObservableCollection<ColorLevelViewModel> _palleteSafe = new ObservableCollection<ColorLevelViewModel>();
        private ColorLevelViewModel _selectedLayer;
        private Visibility _palleteVisibility;

        private RelayCommand _closeCommand;
        private RelayCommand _okCommand;
        private RelayCommand _addLayerCommand;
        private RelayCommand _deleteLayer;

        #endregion // Поля

        #region // Свойства

        [XmlElement]
        public ObservableCollection<ColorLevelViewModel> Pallete
        {
            get
            {
                return _pallete;
            }

            set
            {
                _pallete = value;
            }
        }

        public ColorLevelViewModel SelectedLayer
        {
            get { return _selectedLayer; }
            set
            {
                _selectedLayer = value;
                OnPropertyChanged("SelectedLayer");
            }
        }

        [XmlElement]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        public Visibility PalleteVisibility
        {
            get { return _palleteVisibility; }
            set
            {
                _palleteVisibility = value;
                OnPropertyChanged("PalleteVisibility");

                if (value == Visibility.Visible)
                {
                    _palleteSafe.Clear();
                    foreach (var layer in _pallete)
                    {
                        _palleteSafe.Add(layer);
                    }
                }
            }
        }

        #endregion // Свойства

        #region Конструкторы

        public PalleteViewModel()
        {
            _name = null;
        }

        public PalleteViewModel(string name)
        {
            _name = name;
        }

        #endregion // Конструкторы

        #region CloseCommand

        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand != null)
                    return _closeCommand;

                _closeCommand = new RelayCommand
                    (
                        param => Close()
                    );
                return _closeCommand;
            }
        }

        public void Close()
        {
            PalleteVisibility = Visibility.Hidden;

            _pallete.Clear();
            foreach (var layer in _palleteSafe)
            {
                _pallete.Add(layer);
            }
        }

        #endregion //CloseCommand

        #region OkCommand

        public ICommand OkCommand
        {
            get
            {
                if (_okCommand != null)
                    return _okCommand;

                _okCommand = new RelayCommand
                    (
                        param => Ok()
                    );
                return _okCommand;
            }
        }

        public void Ok()
        {
            PalleteVisibility = Visibility.Hidden;
        }

        #endregion //OkCommand

        #region AddLayerCommand

        public ICommand AddLayerCommand
        {
            get
            {
                if (_addLayerCommand != null)
                    return _addLayerCommand;

                _addLayerCommand = new RelayCommand
                    (
                        param => AddLayer()
                    );
                return _addLayerCommand;
            }
        }

        public void AddLayer()
        {
            SolidColorBrush currentBrush;
            var newColor = new System.Windows.Media.Color {A = 255, R = 255, G = 255, B = 255};
            if (SelectedLayer != null)
            {
                currentBrush =
                    SelectedLayer.Color as SolidColorBrush ??
                    new SolidColorBrush(newColor);
            }
            else
                currentBrush = new SolidColorBrush(newColor);

            var myDialog = new ColorDialog { ShowHelp = true };

            if (myDialog.ShowDialog() != DialogResult.OK)
                return;

            newColor = new System.Windows.Media.Color
                            {
                                A = myDialog.Color.A,
                                R = myDialog.Color.R,
                                G = myDialog.Color.G,
                                B = myDialog.Color.B
                            };

            _pallete.Add(new ColorLevelViewModel(Color.FromArgb(255,255,255,255), 0.0));
        }

        #endregion //AddLayerCommand

        #region DeleteLayerCommand

        public ICommand DeleteLayerCommand
        {
            get
            {
                if (_deleteLayer != null)
                    return _deleteLayer;

                _deleteLayer = new RelayCommand
                    (
                        param => DeleteLayer()
                    );
                return _deleteLayer;
            }
        }

        public void DeleteLayer()
        {
            _pallete.Remove(SelectedLayer);
            OnPropertyChanged("SelectedLayer");
        }

        #endregion //DeleteLayerCommand
    }
}
